package cn.kgc.tangcco.controller;

import cn.hutool.core.util.IdUtil;
import cn.kgc.tangcco.model.*;
import cn.kgc.tangcco.pojo.Clazz;
import cn.kgc.tangcco.pojo.Student;
import cn.kgc.tangcco.service.StudentService;
import cn.kgc.tangcco.utils.cryptography.BaseCryptographyUtils;
import cn.kgc.tangcco.vo.StudentVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 李昊哲
 * @Description
 * @create 2020/12/27 17:15
 */
@RestController
@RequestMapping(value = "/student")
public class StudentHandler {
    @Value("${key.des}")
    private  String deskey;
    @Autowired
    private StudentService studentService;

    @GetMapping(value = "/query4layui/{cid}")
    public LayPage<Student> query4layui(Student student, @PathVariable("cid") Integer cid,
                                        @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                                        @RequestParam(value = "limit", required = false, defaultValue = "10") Integer limit) {
        Clazz clazz = new Clazz();
        if (cid != null) {
            if (cid > 0) {
                clazz.setId(cid);
            }
        } else {
            clazz.setId(null);
        }
        student.setClazz(clazz);
        LayPage<Student> layPage = new LayPage<>();
        Map<String, Object> parmas = new HashMap<>();
        // 查询参数
        parmas.put("student", student);
        parmas.put("pageParam", new PageParam(page, limit));
        Page<Student> pageInfo = studentService.query(parmas);
        layPage.setCount(pageInfo.getTotal());
        layPage.setData(pageInfo.getRecords());
        return layPage;
    }

    @PutMapping(value = "/add")
    public ResponseText add(Student student) {
        student.setUuid(IdUtil.fastSimpleUUID());
        return studentService.addOrUpdate(student);
    }

    @GetMapping(value = "/getStudentById/{id}")
    public ResponseText<Student> getStudentById(@PathVariable("id") Integer id) {

        return studentService.getStudentById(id);
    }
    /**
     * 男女比例
     */
    @GetMapping(value = "/queryPercent4Gender/{id}")
    public Map<String, Object> queryPercent4Gender(@PathVariable("id") Integer id) {
        // 调用业务逻辑 讲获取的结果向页面输出
        List<StudentVo> studentVos = studentService.queryCountGroupByGender(new Clazz(id, null));
        Map<String, Object> map = new HashMap<>();
        // 存放列表
        List<String> genderList = new ArrayList<>();
        // 存放数据
        List<Percent> percentList = new ArrayList<>();
        studentVos.forEach(personVo -> {
            String gender = (personVo.getGender() == 1) ? "男性" : "女性";
            // 列表
            genderList.add(gender);
            // 数据
            percentList.add(new Percent(gender, personVo.getTotal()));
        });
        map.put("genderList", genderList);
        map.put("percentList", percentList);
        return map;
    }

    @GetMapping(value = "/mobileExists/{mobile}")
    public ResponseText mobileExists(@PathVariable("mobile") String mobile) {
        if (mobile == null) {
            return new ResponseText(ResultCode.PARAM_ERROR.getCode(), ResultCode.DATA_ERROR.getMsg(), null);
        }
        return studentService.queryMobileExists(mobile);
    }

    @DeleteMapping(value = "/del")
    public ResponseText del(@RequestBody List<Integer> ids) {
        return studentService.delelteStudent(ids);
    }


    @PutMapping(value = "/update")
    public ResponseText update(Student student) {
        return studentService.addOrUpdate(student);
    }
}
